package com.wangchao.administrator.zenmechi.ui.fragment.buyFragment;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.wangchao.administrator.zenmechi.R;

import java.util.List;

/**
 * Created by cbb on 2016/10/18.
 * Email: 1023151857@qq.com
 */
public class adapterBuy extends RecyclerView.Adapter<adapterBuy.MyViewHolder> {
    private Context context;
    private List<BasketBean> list;
    private ImageView iv_goods, iv_add, iv_minus;
    private TextView tv_goods;
    private TextView tv_price;
    private TextView tv_counts;
    private View view;

    public adapterBuy(Context context, List<BasketBean> list) {
        this.context = context;
        this.list = list;
    }

    public interface onItemClickListener {
        void onItemClick(View view, int position);

        void onItemLongClick(View view, int position);

        void onAddClick(View view, int position);

        void onMinusClick(View view, int position);
    }

    private onItemClickListener mOnItemClickListener;

    public void setmOnItemClickListener(onItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View parentView=LayoutInflater.from(context).inflate(R.layout.shoppingcar_item, parent, false);
        MyViewHolder holder = new MyViewHolder(parentView);
        view = LayoutInflater.from(context).inflate(R.layout.goods_item, parent, false);
        iv_goods = (ImageView) view.findViewById(R.id.iv_goods);
        tv_goods = (TextView) view.findViewById(R.id.tv_goods);
        tv_price = (TextView) view.findViewById(R.id.tv_price);
        tv_counts = (TextView) view.findViewById(R.id.tv_counts);
        iv_add = (ImageView) view.findViewById(R.id.iv_add);
        iv_minus = (ImageView) view.findViewById(R.id.iv_minus);
        return holder;
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {
        holder.tv_market.setText(list.get(position).getMarket_name());
        for (int i = 0; i < list.get(position).getListGoods().size(); i++) {

            Glide.with(context).load(list.get(position).getListGoods().get(i).getPic()).into(iv_goods);
            tv_goods.setText(list.get(position).getListGoods().get(i).getGoods_name());
            tv_price.setText(list.get(position).getListGoods().get(i).getPrice());
            tv_counts.setText(list.get(position).getListGoods().get(i).getCounts() + "");

            holder.rl_shopping_item.addView(view);

        }

        if (mOnItemClickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //int pos = myViewHolder.getLayoutPosition();
                    mOnItemClickListener.onItemClick(holder.itemView, position);
                }
            });

            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    //int pos = myViewHolder.getLayoutPosition();
                    mOnItemClickListener.onItemLongClick(holder.itemView, position);
                    return false;
                }
            });
            iv_add.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mOnItemClickListener.onAddClick(iv_add, position);
                }
            });
            iv_minus.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mOnItemClickListener.onMinusClick(iv_minus, position);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        private TextView tv_market;
        private RelativeLayout rl_shopping_item;

        public MyViewHolder(View itemView) {
            super(itemView);
            rl_shopping_item = (RelativeLayout) itemView.findViewById(R.id.rl_shopping_item);
            tv_market = (TextView) itemView.findViewById(R.id.tv_market);
        }
    }
}
